package syn;

public class UnsafeBuy
{
    public static void main(String[] args)
    {
        BuyTicket bt=new BuyTicket();
        new Thread(bt,"A").start();
        new Thread(bt,"B").start();
        new Thread(bt,"C").start();
    }
}

class BuyTicket implements Runnable
{
    private int ticketNums=1000;
    //不安全private synchronized void buy()
    private synchronized void buy()
    {
        if (ticketNums<=0)
        {
            flag=false;
            return;
        }
        System.out.println(Thread.currentThread().getName()+" get No."+ticketNums--);
    }
    boolean flag=true;
    @Override
    public void run()
    {
        while(flag)
        {
            buy();
        }
    }

}